﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using FarPoint.Web.Spread.Chart;
using FarPoint.Web.Spread;
using System.Text;
using System.Drawing;

/// <summary>
/// This celltype just only be set to a Cell that contain a Sparkline
/// </summary>
public class SparklineEnhanceCellType : GeneralCellType
{
  private ExcelSparkline _sparkline;

  public SparklineEnhanceCellType()
  {
  }

	public SparklineEnhanceCellType(ISparkline sparkline)
	{
    _sparkline = sparkline as ExcelSparkline;
	}

  public override System.Web.UI.Control PaintCell(string id, System.Web.UI.WebControls.TableCell parent, FarPoint.Web.Spread.Appearance style, Inset margin, object value, bool upperLevel)
  {
    Control c = base.PaintCell(id, parent, style, margin, value, upperLevel);

    if (_sparkline != null)
    {
      FpSpread spread = this.GetSpread(parent);
      ISparkline sp;
      ISparklineGroup sparklineGroup;
      if (spread.ActiveSheetView.SparklineContainer.GetSparkline(_sparkline.Row, _sparkline.Column, out sparklineGroup, out sp))
      {
        ExcelSparklineGroup group = sparklineGroup as ExcelSparklineGroup;
        string json = GetSettingJSON(group.Setting as ExcelSparklineSetting);
        parent.Attributes.Add("SparklineSetting", HttpUtility.HtmlEncode(json));

        //add client evetnt handler
        parent.Attributes.Add("onclick", "sparkline_click(this);");
      }
    }

    return c;
  }

  private string GetSettingJSON(ExcelSparklineSetting setting)
  {
    StringBuilder sb = new StringBuilder();

    sb.Append("{");

    //General
    sb.Append("\"ShowAxis\":\"" + setting.DisplayXAxis.ToString().ToLower() + "\",");
    sb.Append("\"PlotDataRightToLeft\":\"" + setting.RightToLeft.ToString().ToLower() + "\",");

    //Markers
    sb.Append("\"ShowMarker\":\"" + setting.ShowMarkers.ToString().ToLower() + "\",");
    sb.Append("\"ShowHigh\":\"" + setting.ShowHigh.ToString().ToLower() + "\",");
    sb.Append("\"ShowLow\":\"" + setting.ShowLow.ToString().ToLower() + "\",");
    sb.Append("\"ShowNegative\":\"" + setting.ShowNegative.ToString().ToLower() + "\",");
    sb.Append("\"ShowFirst\":\"" + setting.ShowFirst.ToString().ToLower() + "\",");
    sb.Append("\"ShowLast\":\"" + setting.ShowLast.ToString().ToLower() + "\",");

    //Markers color
    sb.Append("\"MarkerColor\":\"" + ColorTranslator.ToHtml(setting.MarkersColor) + "\",");
    sb.Append("\"HighColor\":\"" + ColorTranslator.ToHtml(setting.HighMarkerColor) + "\",");
    sb.Append("\"LowColor\":\"" + ColorTranslator.ToHtml(setting.LowMarkerColor) + "\",");
    sb.Append("\"NegativeColor\":\"" + ColorTranslator.ToHtml(setting.NegativeColor) + "\",");
    sb.Append("\"FirstColor\":\"" + ColorTranslator.ToHtml(setting.FirstMarkerColor) + "\",");
    sb.Append("\"LastColor\":\"" + ColorTranslator.ToHtml(setting.LastMarkerColor) + "\"");

    sb.Append("}");

    return sb.ToString();
  }

  private FpSpread GetSpread(Control c)
  {
    Control t = c;
    while (t != null && !(t is Page))
    {
      if (t is FpSpread) return (FpSpread)t;
      t = t.Parent;
    }

    return null;
  }
}